<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-col :span="8">
              <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
                <a-select v-model="model.brand" placeholder="请选择品牌">
                  <a-select-option v-for="(item,index) in brandList" :key="item.id">{{ item.formulaName }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="FC No" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fcNo">
                <a-select v-model="model.fcno" placeholder="请选择FC No">
                  <a-select-option v-for="(item,index) in brandList" :key="item.id">{{ item.fcNo }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="预称重B's水" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-input v-model="model.preWeightBWater" placeholder="请输入预称重B's水" ></a-input>
              </a-form-model-item>
            </a-col>
          </a-col>
        </a-row>
        <div class='add-box'>
          <a-button type="link" icon="plus" @click='addCavity'>添加回溶加量</a-button>
        </div>
                <a-form-model-item :labelCol="labelCol3" :wrapperCol="wrapperCol3">
                  <a-row>
                    <a-col :span="24">
                      <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol3">
                        <div class='mark-infos'>
                          <a-table
                            ref="markTable"
                            size="middle"
                            :scroll="{x:false,y:false}"
                            bordered
                            :rowKey="(record = () => record.id)"
                            :columns="columns"
                            :dataSource="model.eneFormulaPgRepairDetails"
                            :pagination="false"
                            :loading="loading"
                            class="j-table-force-nowrap1">
                            <template slot="action" slot-scope="text, record,index">
                              <a-popconfirm title="确定删除吗?" @confirm="() => deleteCavity(index)">
                                <a>删除</a>
                              </a-popconfirm>
                            </template>
                            <template v-for="col in ['repairNum','repairSolidity','repairWater','adjustWater','finalWater','finalTotal']" :slot="col" slot-scope="text, record, indx">
                              <div :key="record.id">
                            <a-form-model-item v-if="col==='repairNum'">
                                <a-input v-model="record.repairNum"  disabled/>
                            </a-form-model-item>
                            <a-form-model-item v-else-if="col==='repairSolidity'">
                              <a-input v-model="record.repairSolidity" placeholder="请输入回溶固体" @input="onChange(record)"/>
                            </a-form-model-item >
                            <a-form-model-item v-else-if="col==='repairWater'">
                              <a-input v-model="record.repairWater" placeholder="请输入回溶水" disabled/>
                            </a-form-model-item >
                            <a-form-model-item v-else-if="col==='adjustWater'">
                              <a-input v-model="record.adjustWater" placeholder="请输入工艺水调整量" disabled />
                            </a-form-model-item>
                            <a-form-model-item v-else-if="col==='finalWater'">
                              <a-input v-model="record.finalWater" disabled/>
                            </a-form-model-item>
                            <a-form-model-item v-else-if="col==='finalTotal'">
                                <a-input v-model="record.finalTotal" placeholder="请输入最终配料罐重量" />
                            </a-form-model-item>
                              </div>
                            </template>
                          </a-table>
                        </div>
                      </a-form-model-item>
                    </a-col>
                  </a-row>
                </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction, postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'EneFormulaPgRepairForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        eneFormulaPgRepairDetails: [
          {
            id: '',
            repairNum: '',
            repairSolidity: '',
            repairWater: '',
            adjustWater: '',
            finalWater: '',
            finalTotal: ''
          }
        ]
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol3: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol3: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '回溶加量',
          align: 'center',
          dataIndex: 'repairNum',
          scopedSlots: { customRender: 'repairNum' }
        },
        {
          title: '回溶固体',
          align: 'center',
          dataIndex: 'repairSolidity',
          scopedSlots: { customRender: 'repairSolidity' }
        },
        {
          title: '回溶水',
          align: 'center',
          dataIndex: 'repairWater',
          scopedSlots: { customRender: 'repairWater' }
        },
        {
          title: '配料工艺水调整量',
          align: 'center',
          dataIndex: 'adjustWater',
          scopedSlots: { customRender: 'adjustWater' }
        },
        {
          title: '最终工艺水加量',
          align: 'center',
          dataIndex: 'finalWater',
          scopedSlots: { customRender: 'finalWater' }
        },
        {
          title: '最终配料罐重量',
          align: 'center',
          dataIndex: 'finalTotal',
          scopedSlots: { customRender: 'finalTotal' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      loading: false,
      confirmLoading: false,
      brandList:[],// 品牌
      validatorRules: {},
      url: {
        brandList:'/craft/eneFormulaPgBase/list',
        add: '/craft/eneFormulaPgRepair/add',
        edit: '/craft/eneFormulaPgRepair/edit',
        queryById: '/craft/eneFormulaPgRepair/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
    this.getBrandList();
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    getBrandList(){
      getAction(this.url.brandList,{ pageSize: 100 }).then((res) => {
        if(res.success){
          this.brandList = res.result.records;
        }else{
          this.$message.warning(res.message)
        }
      })
    },
    addCavity() {
      if (!this.model.eneFormulaPgRepairDetails) {
        this.$set(this.model, 'eneFormulaPgRepairDetails', [])
      }
      this.model.eneFormulaPgRepairDetails.push({
        id: '',
        repairNum: '',
        repairSolidity: '',
        repairWater: '',
        adjustWater: '',
        finalWater: '',
        finalTotal: ''
      })
    },
    deleteCavity(originalIndex) {
      console.log("iddddddd->",originalIndex)
      console.log("iddddddd",this.model.eneFormulaPgRepairDetails)
      let len = this.model.eneFormulaPgRepairDetails.length
      if (len > 1) {
        this.model.eneFormulaPgRepairDetails.splice(originalIndex, 1)
      } else {
        this.$message.error('最少保留一个回溶加量！')
      }
    },
    onChange(record){
      if(record){
        postAction('/craft/eneFormulaPgRepair/compute/repair',{brand : this.model.brand, preWeightBWater : this.model.preWeightBWater, eneFormulaPgRepairDetail : record}).then(
          (res)=>{
            if(res.success){
              console.log("到这了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊")
              this.$set(record, 'repairNum', res.result.eneFormulaPgRepairDetail.repairNum);
              this.$set(record, 'finalWater', res.result.eneFormulaPgRepairDetail.finalWater);
              this.$set(record, 'adjustWater', res.result.eneFormulaPgRepairDetail.adjustWater);
              this.$set(record, 'repairWater', res.result.eneFormulaPgRepairDetail.repairWater);
            }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>
<style lang="scss" scoped>
.title {
  font-size: 24px;
  font-weight: bold;
}

.unit {
  padding-left: 4px;
  line-height: 40px;
}

.query-group-cust {
  width: 100% !important;
}

.ant-input-number {
  width: 100% !important;
}

.cavity-box {
  .title-box {
    height: 40px;
    padding: 0 5px;
    line-height: 40px;

    span {
      font-size: 24px;
      font-weight: bold;
      float: left;
    }

    .ant-btn-link {
      float: right;
    }
  }

  .mark-infos {
    position: relative;

    .editable-add-btn {
      position: absolute;
      top: 10px;
      right: 18px;
      z-index: 202;
    }
  }
}
::v-deep .ant-table-row {
  height: 48px !important;
}
</style>